import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import '../../bean/bean_train.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class TrainOtherUserCheckRecoderAddPage extends StatefulWidget {
  final TrainUserCheckRecoderEntity trainUserCheckRecoderEntity;

  TrainOtherUserCheckRecoderAddPage({this.trainUserCheckRecoderEntity});

  @override
  _TrainOtherUserCheckRecoderAddPageState createState() =>
      _TrainOtherUserCheckRecoderAddPageState();
}

class _TrainOtherUserCheckRecoderAddPageState
    extends State<TrainOtherUserCheckRecoderAddPage> {
  TextEditingController _editingController1 = TextEditingController();
  TextEditingController _editingController2 = TextEditingController();
  TextEditingController _editingController3 = TextEditingController();
  TextEditingController _editingController4 = TextEditingController();
  TextEditingController _editingController5 = TextEditingController();

  TextEditingController _directorRemarkEditingController =
      TextEditingController();

  @override
  void initState() {
    super.initState();
    if (widget.trainUserCheckRecoderEntity.attendanceNumber != null) {
      _editingController1.text =
          widget.trainUserCheckRecoderEntity.attendanceNumber.toString();
    }
    if (widget.trainUserCheckRecoderEntity.disciplineNumber != null) {
      _editingController2.text =
          widget.trainUserCheckRecoderEntity.disciplineNumber.toString();
    }
    if (widget.trainUserCheckRecoderEntity.studyNumber != null) {
      _editingController3.text =
          widget.trainUserCheckRecoderEntity.studyNumber.toString();
    }
    if (widget.trainUserCheckRecoderEntity.practicalNumber != null) {
      _editingController4.text =
          widget.trainUserCheckRecoderEntity.practicalNumber.toString();
    }
    if (widget.trainUserCheckRecoderEntity.knowledgeNumber != null) {
      _editingController5.text =
          widget.trainUserCheckRecoderEntity.knowledgeNumber.toString();
    }
    if (widget.trainUserCheckRecoderEntity.directorRemark != null) {
      _directorRemarkEditingController.text =
          widget.trainUserCheckRecoderEntity.directorRemark.toString();
    } else {
      _directorRemarkEditingController.text = "合格";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("编辑考评项"),
      ),

      ///填充布局
      body: Container(
          color: Colors.white,
          width: double.infinity,
          padding: EdgeInsets.all(12),
          child: SingleChildScrollView(
            child: Column(
              children: [
                buildRow(
                    leftTitle: "出勤情况", editingController: _editingController1),
                buildRow(
                    leftTitle: "纪律执行", editingController: _editingController2),
                buildRow(
                    leftTitle: "学习表现", editingController: _editingController3),
                buildRow(
                    leftTitle: "实训表现", editingController: _editingController4),
                buildRow(
                    leftTitle: "知识掌握", editingController: _editingController5),
                Container(
                    color: Colors.white,
                    width: double.infinity,
                    height: 300,
                    child: Column(
                      children: [
                        Container(
                          padding: EdgeInsets.all(12),
                          child: TextField(
                            controller: _directorRemarkEditingController,
                            maxLength: 200,
                            maxLines: 8,
                            style: TextStyle(),
                            decoration: InputDecoration(
                                hintText: "请输入班主任鉴定",
                                border: OutlineInputBorder()),
                          ),
                        ),
                      ],
                    )),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    ElevatedButton(
                        onPressed: () {
                          commitNetDataFunction();
                        },
                        child: Text("提交"))
                  ],
                )
              ],
            ),
          )),
    );
  }

  Widget buildRow({String leftTitle, TextEditingController editingController}) {
    return Row(
      children: [
        Text("$leftTitle"),
        SizedBox(
          width: 12,
        ),
        Expanded(
          child: TextField(
            keyboardType: TextInputType.number,
            maxLines: 1,
            controller: editingController,
            decoration: InputDecoration(
                hintText: "请输入$leftTitle的评分 满分15分",
                hintStyle: TextStyle(fontSize: 12)),
            inputFormatters: [
              FilteringTextInputFormatter.allow(RegExp("[0-9]")), //数字包括小数
            ],
          ),
        ),
      ],
    );
  }

  void commitNetDataFunction() async {
    //出勤情况
    String attendanceNumber = _editingController1.text;
    if (attendanceNumber.length == 0) {
      ToastUtils.showToast("请输入出勤情况评分");
      return;
    }
    //纪律执行
    String disciplineNumber = _editingController2.text;
    if (disciplineNumber.length == 0) {
      ToastUtils.showToast("请输入纪律执行评分");
      return;
    }
    //学习表现
    String studyNumber = _editingController3.text;
    if (studyNumber.length == 0) {
      ToastUtils.showToast("请输入学习表现评分");
      return;
    }
    //实训表现
    String practicalNumber = _editingController4.text;
    if (practicalNumber.length == 0) {
      ToastUtils.showToast("请输入实训表现评分");
      return;
    }
    //知识掌握
    String knowledgeNumber = _editingController5.text;
    if (knowledgeNumber.length == 0) {
      ToastUtils.showToast("请输入知识掌握评分");
      return;
    }
    //
    String directorRemark = _directorRemarkEditingController.text;

    Map<String, dynamic> map = {
      "attendanceNumber": attendanceNumber,
      "disciplineNumber": disciplineNumber,
      "studyNumber": studyNumber,
      "practicalNumber": practicalNumber,
      "knowledgeNumber": knowledgeNumber,
      "directorRemark": directorRemark,
      "id": widget.trainUserCheckRecoderEntity.id,
    };
    String url = HttpHelper.userCheckRecoderUpdate;

    ResponseInfo partResponseInfo = await DioUtils.instance.postRequest(
      url: url,
      jsonMap: map,
    );
    ToastUtils.showToast("编辑成功");
    Future.delayed(Duration(milliseconds: 1200), () {
      NavigatorUtils.pop(context);
    });
  }
}
